package com.dfbz.service.impl;

import com.dfbz.dao.ArticleDao;
import com.dfbz.model.Article;
import com.dfbz.model.Favorite;
import com.dfbz.model.User;
import com.dfbz.pojo.PageEntity;
import com.dfbz.service.ArticleService;
import com.dfbz.utils.PageUtil;

import java.util.ArrayList;
import java.util.List;

public class ArticleServiceImpl implements ArticleService {
    ArticleDao articleDao=new ArticleDao();
    @Override
    public Article findById(Integer id) {
        return articleDao.findByid(id);
    }

    @Override
    public ArrayList<Article> findList() {
        return articleDao.findList();
    }

    @Override
    public void save(Article article) {
        articleDao.save(article);
    }

    @Override
    public void deleteById(Integer id) {
        articleDao.deleteById(id);
    }

    @Override
    public void update(Article article) {
        articleDao.update(article);
    }

    @Override
    public PageEntity<Article> findPage(Integer id,Integer currPage, String articleTitle) {
        PageEntity<Article> pageEntity = new PageEntity<>();
        pageEntity.setPageSize(3);
        List<Article> pageData = articleDao.findArticle(id, articleTitle, (currPage - 1) * pageEntity.getPageSize(), pageEntity.getPageSize());
        Integer totalCount = articleDao.countByArticle(id, articleTitle);
        return PageUtil.getPageEntity(pageEntity,currPage,pageData,totalCount);
    }

    @Override
    public PageEntity<Article> findCollectPage(Integer id, Integer currPage, String articleTitle) {
        PageEntity<Article> pageEntity = new PageEntity<>();
        pageEntity.setPageSize(3);
        List<Article> pageData = articleDao.findCollectArticle(id, articleTitle, (currPage - 1) * pageEntity.getPageSize(), pageEntity.getPageSize());
        Integer collectCount = articleDao.countByColArticle(id, articleTitle);
        return PageUtil.getPageEntity(pageEntity,currPage,pageData,collectCount);
    }

    @Override
    public Integer countById(Integer id) {
        return articleDao.countById(id);
    }

    @Override
    public Integer IsfavArticle(Integer uId, Integer aId) {
        return articleDao.IsfavArticle(uId,aId);
    }

    @Override
    public ArrayList<Favorite> favArticleById(Integer aId, Integer uId) {
        return articleDao.favArticleById(aId,uId);
    }
}
